package slap.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import slap.paradigm.Function;


public class Regex {
	
	public static String reg_replace(Pattern pattern, String content, Function<String, Matcher> func){
		//TODO fix this!
		content = content.replace("$", "\\$");
		Matcher matcher = pattern.matcher(content);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, func.invoke(matcher));
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	
	public static String reg_replace(Pattern pattern, String content, String replace){
		content = content.replace("$", "\\$");
		Matcher matcher = pattern.matcher(content);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, replace);
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	
}
